::Copyright [2013] [Eric Lawrey, Australian Institute of Marine Science]
::
::   Licensed under the Apache License, Version 2.0 (the "License");
::   you may not use this file except in compliance with the License.
::   You may obtain a copy of the License at
::
::       http://www.apache.org/licenses/LICENSE-2.0
::
::   Unless required by applicable law or agreed to in writing, software
::   distributed under the License is distributed on an "AS IS" BASIS,
::   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
::   See the License for the specific language governing permissions and
::   limitations under the License.

:: This script uses the eAtlasDataTools R quickStart function to find GIS files
:: in the current path to create an uploadLayers.csv. If run
:: again it uploads the results to the specified GeoServer.
:: This script is useful for getting layers up on the GeoServer quickly. Then subsequently
:: refining the setup of the styles on the GeoServer. Note: This script does not
:: handle the creation of new styles associated with the uploaded data. They must all
:: pre-exist.
:: To run this script you must have the following installed on this machine:
::  - R
::  - R Tools?
::  - eAtlasDataTools library, see https://code.google.com/p/e-atlas-data-tools/

@echo off
setlocal EnableDelayedExpansion
SET GEOSERVER_URL=http://localhost:8080/geoserver
SET USER=admin
SET PASSWORD=geoserver
SET UPLOAD_LAYERS_FILE=uploadLayers.csv
SET SKIPUPLOADS=TRUE
if exist %UPLOAD_LAYERS_FILE% (
	:: If the user hits the enter key then the original value of the variable is unmodified
	SET /P GEOSERVER_URL="Found %UPLOAD_LAYERS_FILE%. GeoServer to upload to (N to Cancel): [!GEOSERVER_URL!]?"
	IF /I "!GEOSERVER_URL!" == "N" GOTO END

	SET /P USER="User: (N to Cancel) [!USER!]?"
	IF /I "!USER!" == "N" GOTO END
	
	SET /P PASSWORD="Password: (N to Cancel) [!PASSWORD!]?"
	IF /I "!PASSWORD!" == "N" GOTO END

	SET /P SKIPUPLOADS="Skip uploading data if it already exists on the GeoServer: (TRUE, FALSE or N to Cancel) [!SKIPUPLOADS!]?"
	IF /I "!SKIPUPLOADS!" == "N" GOTO END
	
	echo "!GEOSERVER_URL! !USER! !PASSWORD!"
	Rscript -e "library("eAtlasDataTools"); geoserverRestLoader('!USER!','!PASSWORD!', '!GEOSERVER_URL!', '!UPLOAD_LAYERS_FILE!', skipUploadIfStoreExists=!SKIPUPLOADS!)"
	
) else (
	echo "Looking for GIS files"
	Rscript -e "library("eAtlasDataTools"); quickStart(layerTableCsv='%UPLOAD_LAYERS_FILE%')"
)

:END
endlocal

pause